/**
 * 
 */
package org.aman.shiro.action;

import javax.servlet.http.HttpServletRequest;

import org.aman.shiro.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Copyright: 本内容仅限于重庆爱赢科技有限公司内部使用，禁止转发. 
 * @Author: daijiangguo 2014-6-11 上午11:49:41 
 * @Version: $Id$
 * @Desc: <p></p>
 */
@Controller
public class LoginAction {

    @ResponseBody
    @RequestMapping(value="/tologin")
    public String tologin(){
        return "login";
    }
    
    @ResponseBody
    @RequestMapping(value="/login")
    public String login(String username, String password, String authcode, Boolean rememberMe,
            HttpServletRequest request, ModelMap model){
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {// 重复登录
            try {
                if (subject.isAuthenticated()) {// 重复登录
                    User user = (User) subject.getPrincipal();
                    if (!user.getUserName().equals(username)) {// 如果登录名不同
                        subject.logout();
                        subject.login(token);
                    }
                } else {
                    subject.login(token);
                }
            } catch (AccountException e) {
                e.printStackTrace();
                return "登录失败错误信息:";
            } catch (AuthenticationException e) {
                e.printStackTrace();
                return "登录失败错误信息:";
            } catch (Exception e){
                e.printStackTrace();
                return e.getMessage();
            }
        }
        return "login";
    }
    @ResponseBody
    @RequestMapping(value="/noauth")
    public String noauth(User user){
        
        return "noauth";
    }
    @ResponseBody
    @RequestMapping(value="/list")
    public String list(User user){
        
        return "list";
    }
    
    @ResponseBody
    @RequestMapping(value="/aman0")
    public String aman0(User user){
        return "aman0";
    }
    @ResponseBody
    @RequestMapping(value="/aman1")
    public String aman1(User user){
        return "aman1";
    }
    @ResponseBody
    @RequestMapping(value="/aman2")
    public String aman2(User user){
        return "aman2";
    }
    @ResponseBody
    @RequestMapping(value="/aman3")
    public String aman3(User user){
        return "aman3";
    }
    @ResponseBody
    @RequestMapping(value="/aman4")
    public String aman4(User user){
        return "aman4";
    }
}
